package com.nsgf.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.ChartDataBean;
import com.nsgf.library.data.bean.TransfusionBean;
import com.nsgf.library.databinding.ItemMarkerChartsBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class MarkerListView extends MarkerView {

    private List<TransfusionBean.ListBean> list = new ArrayList<>();
    private LinearLayout ll_list_marker_charts;

    private List<ChartDataBean> chartDataBeans;
    private Context mContext;
    private TextView tv_date;

    private int type = 0;//0渗流监测 1渗压监测

    private boolean many;//多图例
    private int index;//多图例

    public MarkerListView(Context context,
                          List<TransfusionBean.ListBean> list,
                          List<ChartDataBean> chartDataBeans) {
        super(context, R.layout.transfusion_marker_view);

        this.mContext = context;
        this.list = list;
        tv_date = findViewById(R.id.tv_date);
        ll_list_marker_charts = findViewById(R.id.ll_list_marker_charts);
        this.chartDataBeans = chartDataBeans;
    }

    public MarkerListView(Context context,
                          List<TransfusionBean.ListBean> list,
                          List<ChartDataBean> chartDataBeans, boolean many,int index) {
        super(context, R.layout.transfusion_marker_view);

        this.mContext = context;
        this.list = list;
        tv_date = findViewById(R.id.tv_date);
        ll_list_marker_charts = findViewById(R.id.ll_list_marker_charts);
        this.chartDataBeans = chartDataBeans;
        this.many = many;
        this.index = index;
    }

    /**
     *
     * @param context
     * @param type 数量取值key
     * @param list
     * @param chartDataBeans
     */
    public MarkerListView(Context context, int type,
                          List<TransfusionBean.ListBean> list,
                          List<ChartDataBean> chartDataBeans) {
        super(context, R.layout.transfusion_marker_view);

        this.mContext = context;
        this.type = type;
        this.list = list;
        tv_date = findViewById(R.id.tv_date);
        ll_list_marker_charts = findViewById(R.id.ll_list_marker_charts);
        this.chartDataBeans = chartDataBeans;
    }

    public void setChartDataBeans(List<ChartDataBean> chartDataBeans) {
        this.chartDataBeans = chartDataBeans;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        ll_list_marker_charts.removeAllViews();
        tv_date.setText(list.get((int) e.getX()).getDate());


        if (many){

            View view = View.inflate(mContext, R.layout.item_marker_charts, null);
            ItemMarkerChartsBinding chartsBinding = DataBindingUtil.bind(view);
            if (type == 0) {
                chartsBinding.tvValue.setText((index == 0 ? list.get((int) e.getX()).getValue() : list.get((int) e.getX()).getArtificialValue()) + "L/S");
            } else
                chartsBinding.tvValue.setText(list.get((int) e.getX()).getWaterLevel() + "m");
            chartsBinding.tvTitle.setText(chartDataBeans.get(index).getChartTitles());
            ll_list_marker_charts.addView(view);
        }else{


            for (int i = 0; i < chartDataBeans.size(); i++) {
                if (chartDataBeans.get(i).isChartLineDataSet()) {
                    View view = View.inflate(mContext, R.layout.item_marker_charts, null);
                    ItemMarkerChartsBinding chartsBinding = DataBindingUtil.bind(view);
                    //展示自定义X轴值 后的X轴内容
                    GradientDrawable drawable = new GradientDrawable();
                    drawable.setShape(GradientDrawable.OVAL);
                    drawable.setColor(chartDataBeans.get(i).getColor());
                    chartsBinding.viewColor.setBackground(drawable);

                    if (type == 0) {
                        chartsBinding.tvValue.setText((i == 0 ? list.get((int) e.getX()).getValue() : list.get((int) e.getX()).getArtificialValue()) + "L/S");
                    } else
                        chartsBinding.tvValue.setText(list.get((int) e.getX()).getWaterLevel() + "m");
                    chartsBinding.tvTitle.setText(chartDataBeans.get(i).getChartTitles());
                    ll_list_marker_charts.addView(view);
                }
            }
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
